# 测试程序
import matplotlib.pyplot as plt
import matplotlib as mpl
from matplotlib.patches import Polygon
import numpy as np
import pandas as pd
import math
import datetime
from scipy.interpolate import griddata

#引入色标库
from RadarColorMap import *
#引入CINRAD/SAD数据读取类库
import CinradSAD as SAD

# 程序执行时间：
start = datetime.datetime.now()
#读取基数据文件
filename = r"F:\\20200516\\Z_RADR_I_Z9597_20200516045800_O_DOR_SA_CAP_FMT.bin" #"F:\\724雷达\\VTB20200805091129.014.zip.AR2" #
SADData = SAD.CinradSADData(filename)
# 程序执行时间：
end = datetime.datetime.now()
print(end-start)

# 程序执行时间：
start = datetime.datetime.now()
df_Z, Ref_X, Ref_Y, Ref_H = SADData.GetPPIData(types=[1,2,3,7,9,11], Ele=5, MaxR=230) # 获取数据，数值、X坐标、Y坐标、H坐标（高度）
# 程序执行时间：
end = datetime.datetime.now()
print(end-start)

#准备画布, 2行3列
fig1,axes=plt.subplots(nrows=2,ncols=3,figsize=(20,20),sharey=True, sharex=True)
plt.xlim(-230,230)
plt.ylim(-230,230)

DataType = [0, 1, 2, 6, 5, 7] #数据类型
clolorMaps = [ cm_Ref, cm_Ref, cm_V, cm_ZDR, cm_CC, cm_KDP ] #对应色标
Norms = [ normR, normR, normV, normZDR, normCC, normKDP ] #对应色标等级
Titles = [ "原始反射率", "反射率", "多普勒速度", "差分反射率", "相关系数", "差分相移率" ] #对应标题

# 6个子图
for i in range(2):
    for j in range(3):
        # 获取各类型数据，并画图显示
        index = i*3+j  # 数据类型索引

        C1 = axes[i,j].pcolormesh(Ref_X, Ref_Y, df_Z[index], cmap=clolorMaps[index], norm=Norms[index]) # 用 pcolormesh 画彩色图
        axes[i,j].set_title(Titles[index], fontproperties="SimHei", fontsize=12) #  标题
        axes[i,j].axis([-230, 230, -230, 230]) #坐标范围

        #添加色标条
        cbar_ax = fig1.add_axes([0.36 + 0.27*j,0.55-i*0.4,0.01,0.3]) #色标条位置
        plt.colorbar(C1, cax=cbar_ax)

        # 距圈
        for k in range(50,201,50): # 每50km画一圈
            cir1 = mpl.patches.Circle(xy = (0.0, 0.0), radius=k, linestyle=':', edgecolor = 'gray', facecolor= 'none', alpha=0.5)
            axes[i,j].add_patch(cir1)
        # 最外圈
        cir1 = mpl.patches.Circle(xy = (0.0, 0.0), radius=230, edgecolor = 'black', facecolor= 'none', alpha=0.5)
        axes[i,j].add_patch(cir1)
        #画方位线
        for k in range(30,181,30): # 每30°画一条线
            axes[i,j].plot([230*math.cos(math.radians(k+180)),230*math.cos(math.radians(k))],
                        [230*math.sin(math.radians(k+180)),230*math.sin(math.radians(k))], r':', color='gray', linewidth=0.5)

plt.show()
